﻿Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing

Public Class NetworkTransport
    Private M_type As DataType
    Private M_Data As New List(Of Byte)
    Private M_DataLen As Integer
    Private M_Bytes As Byte()
    Private M_Text As String
    Private M_Bmp As Bitmap
    Private M_RemoteCommand As String
    Private M_RemoteValue As String

    Private M_ContainText As Boolean = False
    Private M_ContainBytes As Boolean = False
    Private M_ContainImage As Boolean = False
    Private M_ContainRemote As Boolean = False
    Public Enum DataType
        Text = 1
        ByteArray = 2
        Image = 3
        Remote = 4
    End Enum
    Public Function Encode(ByVal Data As Byte()) As Byte()
        M_type = DataType.ByteArray
        M_Data.Clear()
        Dim TmpType As Int16 = M_type
        Dim TmpLen As Int64 = Data.Length
        M_Data.AddRange(BitConverter.GetBytes(TmpType))
        M_Data.AddRange(BitConverter.GetBytes(TmpLen))
        M_Data.AddRange(Data)
        Return M_Data.ToArray
    End Function
    Public Function Encode(ByVal Data As String) As Byte()
        M_type = DataType.Text
        M_Data.Clear()
        Dim TmpData As Byte() = Encoding.UTF8.GetBytes(Data)
        Dim TmpType As Int16 = M_type
        Dim TmpLen As Int64 = TmpData.Length
        M_Data.AddRange(BitConverter.GetBytes(TmpType))
        M_Data.AddRange(BitConverter.GetBytes(TmpLen))
        M_Data.AddRange(TmpData)
        Return M_Data.ToArray
    End Function
    Public Function Encode(ByVal Data As Bitmap, Optional ByVal Quality As Integer = 95) As Byte()
        M_type = DataType.Image
        M_Data.Clear()
        Dim M As New System.IO.MemoryStream
        Dim qualityParam As New EncoderParameter(Imaging.Encoder.Quality, Quality)
        Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")
        Dim encoderParams As New EncoderParameters(1)
        encoderParams.Param(0) = qualityParam
        Data.Save(M, jpegCodec, encoderParams)
        Dim TmpData As Byte() = M.ToArray
        Dim TmpType As Int16 = M_type
        Dim TmpLen As Int64 = TmpData.Length
        M_Data.AddRange(BitConverter.GetBytes(TmpType))
        M_Data.AddRange(BitConverter.GetBytes(TmpLen))
        M_Data.AddRange(TmpData)
        Return M_Data.ToArray
    End Function
    Public Function Encode(ByVal Command As String, ByVal Value As String) As Byte()
        Dim Data As String
        Data = Command & ":" & Value
        M_type = DataType.Remote
        M_Data.Clear()
        Dim TmpData As Byte() = Encoding.UTF8.GetBytes(Data)
        Dim TmpType As Int16 = M_type
        Dim TmpLen As Int64 = TmpData.Length
        M_Data.AddRange(BitConverter.GetBytes(TmpType))
        M_Data.AddRange(BitConverter.GetBytes(TmpLen))
        M_Data.AddRange(TmpData)
        Return M_Data.ToArray
    End Function
    Public Function Decode(ByVal Data As Byte()) As Boolean
        Try
            M_type = BitConverter.ToInt16(Data, 0)
            M_Data.Clear()
            Dim TmpLen As Int64 = BitConverter.ToInt64(Data, 2)
            Dim Bytes(TmpLen) As Byte
            Array.Copy(Data, 10, Bytes, 0, TmpLen)
            M_Data.AddRange(Bytes)
            M_ContainBytes = False
            M_ContainImage = False
            M_ContainText = False
            If M_type = DataType.ByteArray Then
                M_Data.CopyTo(Bytes)
                M_ContainBytes = True
                Return True
            End If
            If M_type = DataType.Text Then
                M_Text = Encoding.UTF8.GetString(M_Data.ToArray)
                M_Text = M_Text.Substring(0, M_Text.Length - 1)
                M_ContainText = True
                Return True
            End If
            If M_type = DataType.Image Then
                Dim M As New IO.MemoryStream(M_Data.ToArray)
                M_Bmp = New Bitmap(M)
                M_ContainImage = True
                Return True
            End If
            If M_type = DataType.Remote Then
                Dim M_Temp = Encoding.UTF8.GetString(M_Data.ToArray)
                M_Temp = M_Temp.Substring(0, M_Temp.Length - 1)
                M_RemoteCommand = M_Temp.Substring(0, M_Temp.IndexOf(":"))
                M_RemoteValue = M_Temp.Substring(M_Temp.IndexOf(":") + 1)
                M_ContainRemote = True
                Return True
            End If
            Return False
        Catch
            Return False
        End Try
    End Function
    Public ReadOnly Property RemoteCommand() As String
        Get
            Return M_RemoteCommand
        End Get
    End Property
    Public ReadOnly Property RemoteValue() As String
        Get
            Return M_RemoteValue
        End Get
    End Property
    Public ReadOnly Property Text() As String
        Get
            Return M_Text
        End Get
    End Property
    Public ReadOnly Property Bytes() As Byte()
        Get
            Return M_Bytes
        End Get
    End Property
    Public ReadOnly Property Image() As Bitmap
        Get
            Return M_Bmp
        End Get
    End Property

    Public ReadOnly Property ContainText() As Boolean
        Get
            Return M_ContainText
        End Get
    End Property
    Public ReadOnly Property ContainBytes() As Boolean
        Get
            Return M_ContainBytes
        End Get
    End Property
    Public ReadOnly Property ContainImage() As Boolean
        Get
            Return M_ContainImage
        End Get
    End Property
    Public ReadOnly Property ContainRemote() As Boolean
        Get
            Return M_ContainRemote
        End Get
    End Property
    Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        ' Get image codecs for all image formats
        Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
        ' Find the correct image codec
        For i As Integer = 0 To codecs.Length - 1
            If (codecs(i).MimeType = mimeType) Then
                Return codecs(i)
            End If
        Next i
        Return Nothing
    End Function
End Class
